//
//  WPLoginViewModel.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/8.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPLoginViewModel.h"
#import "UserModelArchiver.h"

@implementation WPLoginViewModel
+ (void)loginWithPhone:(NSString *)phone andPwd:(NSString *)pwd andSuccessBlock:(void (^)(NSString *))succ andFailureBlock:(void (^)(NSString *))fail {
    [NetWorkingManager postWithUrl:@"moveSoftware/login.jhtml" requestParams:[NetWorkingManager paramsByAppendingUserInfo:@{@"username":phone, @"pwd":pwd}] SuccessHandler:^(id responseObject) {
        if (responseObject[@"resultType"] && [responseObject[@"resultType"] intValue]) {
            [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"AutoLogin"];
            [UserModel defaultUser].userID = responseObject[@"resultMsg"][@"id"];
            [UserModel defaultUser].phoneNumber = responseObject[@"resultMsg"][@"username"];
            [UserModel defaultUser].balance = responseObject[@"resultMsg"][@"balance"];
            [UserModel defaultUser].memberRankNumber = responseObject[@"resultMsg"][@"memberRankNumber"];
            [UserModelArchiver archiver];
            succ(@"登陆成功");
        } else {
            fail(responseObject[@"resultMsg"]?responseObject[@"resultMsg"]:@"登录失败");
        }
    } failureHandler:^(NSError *error) {
        fail(error.localizedDescription);
    }];
}
@end
